package ase.eleitweg.server;

import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.rmi.registry.*;

import ase.eleitweg.RMIModuleServer;
import ase.eleitweg.TestDBFilledModule;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class ServerLauncher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
        	Registry registry = LocateRegistry.createRegistry(37445);
        	Injector injector = Guice.createInjector(new TestDBFilledModule(), new RMIModuleServer());
        	//UserLoginController ulc = UserLoginController.getInstance();
            //UserLoginInterface uli = (UserLoginInterface) UnicastRemoteObject.exportObject(ulc, 0);
        	//UserLoginInterface uli = injector.getInstance(UserLoginInterface.class);
        	UserLoginInterface uli = (UserLoginInterface) UnicastRemoteObject.exportObject(injector.getInstance(UserLoginInterface.class), 0);
        	registry.rebind(UserLoginInterface.rminame, uli);
            
            registry.rebind("userlogininterface",  uli);
            
            //AdminLoginInterface alc = AdminLoginController.getInstance();
            //AdminLoginInterface ali = (AdminLoginInterface) UnicastRemoteObject.exportObject(alc, 0);
            //AdminLoginInterface ali = injector.getInstance(AdminLoginInterface.class);
            AdminLoginInterface ali = (AdminLoginInterface) UnicastRemoteObject.exportObject(injector.getInstance(AdminLoginInterface.class), 0);
            registry.rebind(AdminLoginInterface.rminame, ali);
            
            System.out.println("login interfaces bound");
            System.out.println("press any key to exit.");
            System.in.read();
            
            registry.unbind("userlogininterface");
            registry.unbind("adminlogininterface");
            UnicastRemoteObject.unexportObject(uli, true);
            UnicastRemoteObject.unexportObject(ali, true);
            
        } catch (NotBoundException nbe) {
        	//strange... there's nothing to do anyway.            
            
        } catch (Exception e) {
            System.err.println("server exception:");
            e.printStackTrace();
        }
    }

}
